package stx.com.controller;

import java.io.File;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;




import stx.com.entity.TUser;
import stx.com.service.UserService;
import stx.com.util.Constans;

@Controller
public class RegistController {

	@Resource
	private UserService userService;
	
	/*
	 * 注册
	 */
	@RequestMapping("regist.do")
	public String regist(@RequestParam("photo") MultipartFile file,TUser user,HttpServletRequest request){
//		System.out.println("已到达");
		if(file!=null&&!file.isEmpty()){
			File dir = new File(request.getSession().getServletContext().getRealPath("/")+Constans.STORE_PATH);
//			System.out.println(request.getSession().getServletContext().getRealPath("/")+Constans.STORE_PATH);
			String fileName=file.getOriginalFilename();
			try {
				file.transferTo(new File(dir,fileName));
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			user.setRoute(Constans.STORE_PATH+fileName);
			if(userService.uRegist(user)){
				request.setAttribute("photo1", user);
				return "login";
			}			
		}
		return "false";
	}
	
	@RequestMapping("reg.do")
	public String hah(){
		return "login";
	}
	
}
